/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;


import java.util.List;
import javax.persistence.*;
import jpapractica.*;

public class MovieManager {
    
    EntityManagerFactory emf;
    EntityManager managerMovie;
    
    public MovieManager(){
        emf= Persistence.createEntityManagerFactory("GRLUPU");
        managerMovie= emf.createEntityManager();
    }
    
    public Movie create (int id, String name, int year, String cover, String plotkey
            , String genero, float duration, boolean rental, boolean purchase){
            
            Movie pelicula = new Movie(id, name, year, cover, plotkey, genero, 
                    duration, duration, rental, purchase);
            
        managerMovie.getTransaction().begin();
        managerMovie.persist(pelicula);
        managerMovie.getTransaction().commit();
            
            return pelicula;
                    }
    public void delete(int id){
        Movie pelicula = managerMovie.find(Movie.class, id);
        managerMovie.getTransaction().begin();
        managerMovie.remove(pelicula);
        managerMovie.getTransaction().commit();
    }
    public List<Movie> buscar(String palabra){
   
        Query busqueda =managerMovie.createQuery("SELECT p FROM Movie p WHERE p.name='"+palabra+"'");
        List<Movie> aux =busqueda.getResultList();
        
         return aux;
    }
    public void update(int id, String name, int year, String cover, String plotkey
            , String genero, float duration, boolean rental, boolean purchase){
        Movie pelicula = managerMovie.find(Movie.class, id);
        managerMovie.getTransaction().begin();
        pelicula.setName(name);
        pelicula.setGenero(genero);
        pelicula.setDuration(duration);
        pelicula.setYearM(year);
        pelicula.setPlotkeywords(plotkey);
        pelicula.setAvailableForRental(rental);
        pelicula.setAvailableForPurchase(purchase);
        managerMovie.getTransaction().commit();
        
    
    }
    
}
    

